package com.monster.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.monster.mapper.OrderMapper;
import com.monster.pojo.Member;
import com.monster.pojo.Order;
import com.monster.service.MemberService;
import com.monster.service.OrderService;
import com.monster.service.SetMealService;
import com.monster.utils.date.DateUtils;
import com.monster.utils.redis.RedisUtil;
import com.monster.utils.resources.RedisConstant;
import com.monster.vo.SuccessVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Reference
    OrderService orderService;

    @Reference
    MemberService memberService;

    @Reference
    SetMealService setMealService;

    @Override
    @LcnTransaction
    public Integer sumbit(Map map) {
        //首先判断验证码是否正确
        //思路：将错误的逐个排除
        String validateCode = (String)map.get("validateCode");
        String telephone = (String)map.get("telephone");
        String TrueCode = RedisUtil.get(RedisConstant.ALL_SETMEAL_PIC_SET + telephone);
        if(!TrueCode.equals(validateCode)){
            throw new RuntimeException("验证码不正确");
        }
        //判断是否是新老用户
        String idCard = (String)map.get("idCard");
        Member exitNumber = memberService.IsExitMember(idCard);
        String orderDate = (String) map.get("orderDate");
        String setmealId = (String) map.get("setmealId");
        int memberID;
        //判断当天的预约是否已经满了
        Integer isMore = setMealService.isMore(setmealId);
        if(isMore <= 0){
            throw new RuntimeException(orderDate+"已经预约完，请择日选择");
        }
        if(exitNumber == null){
            //是一个新用户,进行添加用户
            String name = (String)map.get("name");
            String sex = (String)map.get("sex");
            Member member = new Member();
            member.setName(name);
            member.setSex(sex);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date(System.currentTimeMillis()));
            memberID  = memberService.saveMemberService(member);
        }else{
            //这是个老用户,判断在同一天用户是否已经申请过了
            memberID = exitNumber.getId();
            Boolean isExitOrder = baseMapper.findOrderIsExitBySetmealIdAndOrderDateAndTime(memberID,setmealId, orderDate);
            if(isExitOrder){
                throw new RuntimeException("您已注册过，请择日");
            }
        }
        //表数据的添加 `t_order`表的数据添加，`t_ordersetting`表数据修改
        Order order = new Order();
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setOrderType("微信预约");
        order.setOrderStatus("未就诊");
        order.setMemberId(memberID);
        order.setSetmealId(Integer.parseInt(setmealId));
        orderService.save(order);
        baseMapper.updateOrderSetting(setmealId);
        return memberID;
    }

    @Override
    public Map showSuccessPage(int id) {
        return baseMapper.showSuccessPage(id);
    }

}
